package appx.craft.droid.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONException;
import org.json.JSONObject;

import appx.craft.droid.bean.JSONParams;

/**
 * 
 * JSON Parser help to parse the json object from server send 
 *
 */
public class JSONParser {
	
	private String TAG = JSONParser.class.getCanonicalName();
	
	static InputStream is = null;
	static JSONObject jObj = null;
	static String json = "";

	// constructor
	public JSONParser() {

	}

	public JSONObject getJSONFromUrl(String url,ArrayList<JSONParams> mArrayParams) {

		// Making HTTP request
		try {
			Log.d(TAG, "[ getJSONFromUrl ]" + url);
		   	HttpParams mHttpParameters = new BasicHttpParams();
            // Set the timeout in milliseconds until a connection is established.
    		int timeoutConnection = 300000;
    		HttpConnectionParams.setConnectionTimeout(mHttpParameters, timeoutConnection);
    		int timeoutSocket = 300000;
    		HttpConnectionParams.setSoTimeout(mHttpParameters, timeoutSocket);
	    		
			// defaultHttpClient
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(url);
			httpPost.setHeader( "Content-Type", "application/json" );
			
			JSONObject mJSONObject = new JSONObject();    
			if(mArrayParams != null){
				for(int y=0;y<mArrayParams.size();y++){
					JSONParams mobj$Params = mArrayParams.get(y);
					mJSONObject.put(mobj$Params.getStrKey(),mobj$Params.getStrvalue());
				}
			}
			
			StringEntity se = new StringEntity(mJSONObject.toString());
			se.setContentType("application/json");
		    
		    httpPost.setEntity(se); 
			
			HttpResponse httpResponse = httpClient.execute(httpPost);
			int status = httpResponse.getStatusLine().getStatusCode();
			Log.d(TAG,"[ respons ] "+status);
			HttpEntity httpEntity = httpResponse.getEntity();
			is = httpEntity.getContent();

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			Log.error(TAG,  e.getMessage());
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			Log.error(TAG,  e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			Log.error(TAG,  e.getMessage());
		}catch (JSONException e) {
			e.printStackTrace();
			Log.error(TAG,  e.getMessage());
		}

		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(is));
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();
			json = sb.toString();
		} catch (Exception e) {
			Log.d("Buffer Error", "Error converting result " + e.getMessage());
		}

		// try parse the string to a JSON object
		try {
			jObj = new JSONObject(json);
		} catch (JSONException e) {
			Log.d("JSON Parser", "Error parsing data " +  e.getMessage());
		}

		// return JSON String
		return jObj;

	}
	
}
